<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once("../conn.php");
require_once 'common.php';

$module = checkTrim($_GET['module']);

if ($module == "resource") {
    $os = $_POST['os'];
    $version = $_POST['version'];
    $titlePacket = $_POST['titlePacket'];
    $name = $_POST['name'];
    $url = $_POST['url'];

    $data = array();
    $num = count($os);
    for ($i = 0; $i < $num; $i++) {
        $row = array();
        $row['name'] = $name[$i];
        $row['titlePacket'] = $titlePacket[$i];
        $row['os'] = $os[$i];
        $row['version'] = $version[$i];
        $row['url'] = $url[$i];
        if ($name[$i] != "" && $os[$i] != "" && $version[$i] != "" && $url[$i] != "") {
            array_push($data, $row);
        }
    }

//    $file = "http://hibaby.sharemysound.com/server/child/resourceInterface.php?method=set&data=" . json_encode($data);
//    $return = file_get_contents($file);
    if (checkArrayNotEmpty($data)) {
        $db = new DBC();
        $sql = "DELETE FROM t_hibaby_resource_version";
        $return = $db->execute($sql);
        //  名称 	  版本 	操作系统 	URL
        foreach ($data as $row) {
            $sql = "INSERT INTO t_hibaby_resource_version(name,titlePacket,os,version,url) VALUES('" . $row['name'] . "','" . $row['titlePacket'] . "','" . $row['os'] . "','" . $row['version'] . "','" . $row['url'] . "')";
            if (!$db->execute($sql)) {
                $return = false;
            }
        }
    }
    if ($return || $return == "true") {
        $msg = "保存成功";
        echo "<script language='javascript'>alert('" . $msg . "'); </script>";
        $transfer = "resourceList.php?menu=resource";
        echo "<script language='javascript'>location='" . $transfer . "';</script>";
    } else {
        $msg = "操作错误";
        echo "<script language='javascript'>alert('" . $msg . "'); </script>";
        echo "<script language='javascript'>history.back();</script>";
    }
} else if ($module == "server") {
    $ip = $_POST['ip'];
    $port = $_POST['port'];
    $name = $_POST['name'];
    $maxUser = $_POST['maxUser'];
    $status = $_POST['status'];
    $data = array();
    $num = count($ip);
    for ($i = 0; $i < $num; $i++) {
        $row = array();
        $row['name'] = $name[$i];
        $row['ip'] = $ip[$i];
        $row['port'] = $port[$i];
        $row['maxUser'] = $maxUser[$i];
        $row['status'] = $status[$i];
        if ($ip[$i] != "" && $port[$i] != "" && $maxUser[$i] != "") {
            if (!isset($row['status']) || $row['status'] == "") {
                $row['status'] = "0";
            }
            array_push($data, $row);
        }
    }
    /*
      $file = "http://hibaby.sharemysound.com/server/child/serverInterface.php?method=set&data=" . json_encode($data);
      $return = file_get_contents($file);
     */
    $return = false;
    if (checkArrayNotEmpty($data)) {
        $db = new DBC();
        $sql = "DELETE FROM t_hibaby_server";
        $return = $db->execute($sql);
        foreach ($data as $row) {
            $sql = "INSERT INTO t_hibaby_server(ip,port,name,status,maxUser) VALUES('" . $row['ip'] . "','" . $row['port'] . "','" . $row['name'] . "','" . $row['status'] . "','" . $row['maxUser'] . "')";
            if (!$db->execute($sql)) {
                $return = false;
            }
        }
    }

    if ($return || $return == "true") {
        $msg = "保存成功";
        echo "<script language='javascript'>alert('" . $msg . "'); </script>";
        $transfer = "serverList.php?menu=server";
        echo "<script language='javascript'>location='" . $transfer . "';</script>";
    } else {
        $msg = "操作错误";
        echo "<script language='javascript'>alert('" . $msg . "'); </script>";
        echo "<script language='javascript'>history.back();</script>";
    }
} else if ($module == "changeStatus") {
    /*
      $file = "http://hibaby.sharemysound.com/server/child/serverInterface.php?method=changeStatus&ip=" . $_GET['ip'] . "&status=" . $_GET['status'];
      $return = file_get_contents($file);
     */
    $ip = $_GET['ip'];
    $status = $_GET['status'];
    $db = new DBC();
    if ($status == "start") {
        $status = "1";
    } else if ($status == "stop") {
        $status = "0";
    }
    $sql = "UPDATE t_hibaby_server SET status = '" . $status . "' WHERE ip = '" . $ip . "'";
    $return = $db->execute($sql);

    if ($return || $return == "true") {
        $transfer = "serverList.php?menu=server";
        echo "<script language='javascript'>location='" . $transfer . "';</script>";
    } else {
        $msg = "操作错误";
        echo "<script language='javascript'>alert('" . $msg . "'); </script>";
        echo "<script language='javascript'>history.back();</script>";
    }
}

function checkTrim($item) {
    if (isset($item)) {
        $item = trim($item);
    } else {
        $item = "";
    }
    return $item;
}
?>
